<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="物业名称" prop="pmid">
        <el-select v-model="queryParams.pmid" placeholder="请选择物业名称" @change="clearCarshomeList"
                   clearable>
          <el-option
            v-for="item in this.propertyList"
            :key="item.pmid"
            :label="item.propertyName"
            :value="item.pmid">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="车场名称" prop="chid">
        <el-select v-model="queryParams.chid" placeholder="请选择车场名称" @visible-change="getListCars" clearable>
          <el-option
            v-for="item in this.carshomeList"
            :key="item.chid"
            :label="item.chname"
            :value="item.chid">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="车位编号" prop="sparkcode">
        <el-input
          v-model="queryParams.sparkcode"
          placeholder="请输入停车位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="购买情况" prop="purchasesid">
        <el-select v-model="queryParams.purchasesid" placeholder="请选择购买情况"
                   clearable
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in purchasesOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sys-cars-home:parking:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sys-cars-home:parking:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys-cars-home:parking:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sys-cars-home:parking:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="车位id" align="center" prop="chpid" width="100px"/>

      <el-table-column label="物业名称" align="center" prop="propertyManagement.propertyName" width="150px"/>
      <el-table-column label="车场名称" align="center" prop="carsHome.chname" width="150px"/>

      <el-table-column label="停车位编号" align="center" prop="sparkcode" width="100px"/>

      <el-table-column label="车位买主" align="center" prop="buyers" width="100px"/>
      <el-table-column label="门牌号" align="center" prop="number" width="100px"/>

      <el-table-column label="购买情况" align="center" prop="purchasesid" :formatter="showgoumai" width="100px"/>
      <el-table-column label="停车位尺寸" align="center" prop="ctid" :formatter="showsize" width="100px"/>


      <el-table-column label="停车状态" align="center" prop="sparkstatus" :formatter="showstatus" width="100px"/>
      <el-table-column label="当前车辆" align="center" prop="sparkstatus" :formatter="showstatusinfo" width="100px"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleZhanyong(scope.row)"
            v-hasPermi="['sys-cars-home:parking:edit']"
          >车位操作
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
            v-hasPermi="['sys-cars-home:parking:edit']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sys-cars-home:parking:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sys-cars-home:parking:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {
  listParking,
  getParking,
  delParking,
  addParking,
  updateParking
} from "@/api/modules/sys-cars-home/parking/parking";
import {listHome} from "@/api/modules/sys-cars-home/home";
import {listProperty} from "@/api/modules/sys-property/property";

export default {
  name: "Parking",
  data() {
    return {
      // 车场管理表格数据
      carshomeList: [],
      carshomeList2: [],

      // 物业数据
      propertyList: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车场车位表格数据
      parkingList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        sparkcode: null,
        purchasesid: null,
        ctid: null,
        arrangeid: null,
        charge: null,
        source: null,
        sparkstatus: null,
        chid: null,
        buyers: null,
        number: null,
        buyertype: null,

      },
      sparkstatusinfo: null,
      // 查询参数
      queryParams2: {
        pmid: null,
      },
      messaged: null,
      messagedinfo: null,
      purchasesOptions: [{
        "label": "空闲",
        "value": 0
      }, {
        "label": "已被购买",
        "value": 1
      }, {
        "label": "已被租赁",
        "value": 2
      }, {
        "label": "审核中",
        "value": 3
      }],

      operate: null,
    };
  },
  created() {
    this.getList();
    this.getListCars()
    this.getListProperty()
  },
  methods: {

    // anniu(){
    //   if (this.parkingList[0].sparkstatus == 0) {
    //     this.form.sparkstatus = 1
    //     this.messaged="占用车位"
    //   } else if (row.sparkstatus == 1) {
    //     this.form.sparkstatus = 0
    //     this.messaged="释放车位"
    //   }
    // },

    showstatus(row, column, cellValue, index,) {
      if (cellValue) {
        return "正在停车";
      }
      return "空闲";
    },
    showstatusinfo(row, column, cellValue, index,) {
      if (cellValue) {
        return "手动占用";
      }
      return "";
    },

    showsize(row, column, cellValue, index,) {
      if (cellValue) {
        return "大型车";
      }
      return "小型车";
    },

    showgoumai(row, column, cellValue, index,) {

      return cellValue == 0 ? "空闲" : cellValue == 1 ? "已被购买" : cellValue == 2 ? "已被租赁" : "审核中"

    },

    /** 查询车场管理列表 */
    getListCars() {
      this.queryParams2.pmid = this.queryParams.pmid
      listHome(this.queryParams2).then(response => {
        this.carshomeList = response.rows;
      });
    },

    /** 查询物业列表 */
    getListProperty() {
      this.loading = true;
      listProperty(null).then(response => {
        this.propertyList = response.rows;
        this.loading = false;
      });
    },

    //清除车场
    clearCarshomeList() {
      this.queryParams2.pmid = this.queryParams.pmid
      this.queryParams.chid = null
      this.getListCars()
    },


    /** 查询车场车位列表 */
    getList() {
      this.loading = true;
      listParking(this.queryParams).then(response => {
        this.parkingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chpid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        sparkcode: null,
        purchasesid: null,
        ctid: null,
        arrangeid: null,
        charge: null,
        source: null,
        sparkstatus: null,
        chid: null,
        buyers: null,
        number: null,
        buyertype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams.purchasesid)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chpid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 手动占用按钮操作 */
    handleZhanyong(row) {
      this.reset();
      const chpid = row.chpid || this.ids
      this.form.chpid = chpid

      if (row.sparkstatus == 0) {
        this.form.sparkstatus = 1
        this.messaged = "占用车位"
      } else if (row.sparkstatus == 1) {
        this.form.sparkstatus = 0
        this.messaged = "释放车位"
      }


      updateParking(this.form).then(response => {
        this.$modal.msgSuccess(this.messaged + "成功!");
        this.open = false;
        this.getList();
      });

    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: "parkedit", query: {operate: "新增"}})
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chpid = row.chpid || this.ids


      this.$router.push({
        path: 'parkedit',
        query: {
          chpid: chpid,
          operate: "修改",
        }
      });

    },


    /** 修改按钮操作 */
    handleLook(row) {
      this.reset();
      const chpid = row.chpid || this.ids


      this.$router.push({
        path: 'parkedit',
        query: {
          chpid: chpid,
          operate: "查看",
        }
      });

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const chpids = row.chpid || this.ids;
      this.$modal.confirm('是否确认删除车场车位编号为"' + chpids + '"的数据项？').then(function () {
        return delParking(chpids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys-cars-home/parking/export', {
        ...this.queryParams
      }, `parking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
